package com.kee.server.web;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kee.server.job.ExamJob;
import com.kee.server.job.TikuExamJob;

@SuppressWarnings("serial")
public class HttpServletRequestWrapper extends HttpServlet{
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		System.out.println("time:" + config.getInitParameter("time"));
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		String pathInfo = request.getServletPath();
		System.out.println("pahtInfo:" + pathInfo);
		if (pathInfo.startsWith("/")) {
			pathInfo = pathInfo.substring(1);
		}
		ServletOutputStream os = response.getOutputStream();
		StringBuilder repsb = new StringBuilder();
		//菁优网请求
		if (pathInfo.equals(RequestInfo.JINGYOUEJOB.getInfo())) {
			ExamJob job = new ExamJob();
			if (!job.isAlive()) {
				repsb.append("启动完成");
				job.start();
			} else {
				repsb.append("线程已经在运行");
			}
			// 题库请求
		} else if (pathInfo.equals(RequestInfo.TIKEJOB.getInfo())) {
			TikuExamJob tj = new TikuExamJob();
			if (!tj.isAlive()) {
				repsb.append("启动完成");
				tj.start();
			} else {
				repsb.append("线程已经在运行");
			}
		}
		os.write(repsb.toString().getBytes());
		os.close();
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getMethod();
		super.service(request, response);
	}
	
	
	public static enum RequestInfo{
		
		JINGYOUEJOB("jingyou.do"),TIKEJOB("tiku.do");
		
		private String info;
		
		RequestInfo(String info){
			this.info = info;
		}

		public String getInfo() {
			return info;
		}

		public void setInfo(String info) {
			this.info = info;
		}
		
	}
}
